package com.atguigu.tingshu.user.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;

/**
 * ClassName: PrivateKeyConfig
 * Package: com.atguigu.tingshu.user.config
 * Description:项目在启动时加载密钥文件中的私钥到容器中
 */
@Configuration
public class PrivateKeyConfig {

    /**
     * 加载私钥
     *
     * @return
     */
    @Bean
    public RSAPrivateKey rsaPrivateKey(){
        //加载密钥文件
        ClassPathResource classPathResource = new ClassPathResource("tingshu.jks");
        //获取文件中的密钥
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(classPathResource,"tingshu".toCharArray());
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("tingshu", "tingshu".toCharArray());
        //获取私钥
        return (RSAPrivateKey) keyPair.getPrivate();
    }
}
